﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using helper;
namespace Manager
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }
        public static string connStr = ConfigurationManager.AppSettings["connStr"];
        private void label37_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            string Versions = Tex_versions.Text.Trim();
            string Add = tex_addr.Text.Trim();
            string gonggao = tex_gonggao.Text.Trim();
            string sql = "update sys set banben=@banben,dizhi=@dizhi,gonggao=@gonggao where id='1'";
            SqlParameter[] para = new SqlParameter[]
            {
                 new SqlParameter("@banben",Versions),
                 new SqlParameter("@dizhi",Add),
                 new SqlParameter("@gonggao",gonggao)
            };
            if (helper.helper.Result(sql, para))
            {
                MessageBox.Show("修改成功！");
            }
        }

        private void Main_Load(object sender, EventArgs e)
        {
            Tex_versions.Text= helper.helper.showupdate("1", "banben");
            tex_addr.Text = helper.helper.showupdate("2","dizhi");
            tex_gonggao.Text = helper.helper.showupdate("3", "gonggao");
            tbx_ZengSongShiJian.Text = helper.helper.showupdate("4", "songsj");
            switch (helper.helper.showupdate("5","moshi"))
            {
                case "1":
                    cbb_XiTongMoShi.Text = "收费模式";break;
                case "2":
                    cbb_XiTongMoShi.Text = "免费模式";break;
                case "3":
                    cbb_XiTongMoShi.Text = "指定免费";break;
                case "4":
                    cbb_XiTongMoShi.Text = "关闭服务";break;
                default:
                    break;
            }
            tbx_mfsjd.Text = helper.helper.showupdate("6", "zdsj");
            switch (helper.helper.showupdate("7","zcms"))
            {
                case "1":
                    cbb_ZhuCheMoShi.Text = "完全开放";break;
                case "2":
                    cbb_ZhuCheMoShi.Text = "机器码唯一";break;
                case "3":
                    cbb_ZhuCheMoShi.Text = "指定时段";break;
                case "4":
                    cbb_ZhuCheMoShi.Text = "关闭注册";break;
                default:
                    break;
            }
            tbx_zcsjd.Text = helper.helper.showupdate("8", "zcsd");
            tex_dop.Text = helper.helper.showupdate("9","katou");
            string sql = "select * from conn";//连接基本设置的conn
            dataGridView_set.DataSource = helper.helper.DataSet(sql);
      


        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Tex_versions.Clear();
            tex_addr.Clear();
            tex_gonggao.Clear();
            Tex_versions.Text = helper.helper.showupdate("1", "banben");
            tex_addr.Text = helper.helper.showupdate("2", "dizhi");
            tex_gonggao.Text = helper.helper.showupdate("3", "gonggao");
            MessageBox.Show("刷新成功！");
        }
        private void btn_SettingOk_Click(object sender, EventArgs e)
        {
            //连接数据库
            //获取设置
            //修改。ok
            int  zssj = Convert.ToInt32( tbx_ZengSongShiJian.Text);
            int xtms = Xtms();
            string mfsd = tbx_mfsjd.Text;
            int zcms = Zcms();
            //string zcms = cbb_ZhuCheMoShi.Text;
            string zcsd = tbx_zcsjd.Text;
            string czkt = tex_dop.Text;
            string sql = "update sys set songsj=@songsj,moshi=@moshi,zdsj=@zcsj,zcms=@zcms,zcsd=@zcsd,katou=@katou where id=1";
            SqlParameter[] para = new SqlParameter[]
            {
                new SqlParameter("@songsj",zssj),
                new SqlParameter("@moshi",xtms),
                new SqlParameter("@zcsj",mfsd),
                new SqlParameter("@zcms",zcms),
                new SqlParameter("@zcsd",zcsd),
                new SqlParameter("@katou",czkt)
            };
            if (helper.helper.Result(sql, para))
            {
                MessageBox.Show("修改成功！");
            }
        }
      public int Xtms()
        {
            int xtms = 0;
            if (cbb_XiTongMoShi.Text == "收费模式")
            {
                xtms = 1;
            }
            else if (cbb_XiTongMoShi.Text == "免费模式")
            {
                xtms = 2;
            }
            else if (cbb_XiTongMoShi.Text == "指定免费")
            {
                xtms = 3;
            }
            else if (cbb_XiTongMoShi.Text == "关闭服务")
            {
                xtms = 4;
            }
            return xtms;
        }
      public int Zcms()
        { int zcms = 0;
            switch (cbb_ZhuCheMoShi.Text)
            {
                case "完全开放":
                    zcms = 1;break;
                case "机器唯一":
                    zcms = 2;break;
                case "指定时段":
                    zcms = 3;break;
                case "关闭注册":
                    zcms = 4;break;
            
                default:
                    break;
            }
            return zcms;
        }


        private void label11_Click(object sender, EventArgs e)
        {
            tbx_ZengSongShiJian.Clear();
            tbx_mfsjd.Clear();
            tbx_zcsjd.Clear();
            tex_dop.Clear();
            tbx_ZengSongShiJian.Text = helper.helper.showupdate("4", "songsj");
            switch (helper.helper.showupdate("5", "moshi"))
            {
                case "1":
                    cbb_XiTongMoShi.Text = "收费模式"; break;
                case "2":
                    cbb_XiTongMoShi.Text = "免费模式"; break;
                case "3":
                    cbb_XiTongMoShi.Text = "指定免费"; break;
                case "4":
                    cbb_XiTongMoShi.Text = "关闭服务"; break;
                default:
                    break;
            }
            tbx_mfsjd.Text = helper.helper.showupdate("6", "zdsj");
            switch (helper.helper.showupdate("7", "zcms"))
            {
                case "1":
                    cbb_ZhuCheMoShi.Text = "完全开放"; break;
                case "2":
                    cbb_ZhuCheMoShi.Text = "机器码唯一"; break;
                case "3":
                    cbb_ZhuCheMoShi.Text = "指定时段"; break;
                case "4":
                    cbb_ZhuCheMoShi.Text = "关闭注册"; break;
                default:
                    break;
            }
            tbx_zcsjd.Text = helper.helper.showupdate("8", "zcsd");
            tex_dop.Text = helper.helper.showupdate("9", "katou");
            MessageBox.Show("刷新成功！");
        }

        private void bu_update_Click(object sender, EventArgs e)
        {
           
            string klmc = tex_Namespace.Text.Trim();
            string lx = tex_type.Text;
            string bd = Bd();
            string sj = tex_day.Text;
            string kzcs = tex_number.Text;
            string ksj = tex_time.Text;
            string sql = string.Format("update conn set lx=@lx,sj=@sj,bd=@bd,kzcs=@kzcs,ksj=@ksj where klmc=@klmc");
            SqlParameter[] para = new SqlParameter[]
            {
               new SqlParameter("@lx",lx),
               new SqlParameter("@sj",sj),
               new SqlParameter("@bd",bd),
               new SqlParameter("@kzcs",kzcs),
               new SqlParameter("@ksj",ksj),
               new SqlParameter("@klmc",klmc)

            };
            if (helper.helper.Result(sql, para))
            {
                MessageBox.Show("修改成功！");
            }
            else
            {
                MessageBox.Show("修改失败！");
            }
        }
        public string Bd()
        {
           
            switch (com_bound.Text)
            {
                case "0":
                  com_bound.Text =" 无";break;
                case "1":
                    com_bound.Text = "机器码";break;
                case "2":
                    com_bound.Text = "IP";break;
                default: break;
            }
            return com_bound.Text;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string sql = "select * from conn";
           dataGridView_set.DataSource= helper.helper.DataSet(sql);
        }

        private void but_add_Click(object sender, EventArgs e)
        { Random rd = new Random();
            string klmc = tex_Namespace.Text.Trim();
            string lx = tex_type.Text;
            string bd = Bd();
            string sj = tex_day.Text;
            string kzcs = tex_number.Text;
            string ksj = tex_time.Text;
            int id = Id();
            string sql =string.Format( "insert into conn values (@id,@klmc,@lx,@sj,@bd,@kzcs,@ksj)");
            SqlParameter[] para = new SqlParameter[]
            { new SqlParameter("@id",id),
               new SqlParameter("@klmc",klmc),
               new SqlParameter("@lx",lx),
               new SqlParameter("@bd",bd),
               new SqlParameter("@sj",sj),
               new SqlParameter("@kzcs",kzcs),
               new SqlParameter("@ksj",ksj)
            };
            if (helper.helper.Result(sql,para))
            {
                MessageBox.Show("修改成功！");
            }
            else
            {
                MessageBox.Show("修改失败！");
            }
        }
        public int Id()
        {
            int x = 0;
            Random rd = new Random();
            int id = rd.Next(1, 9999);
            string sql = "select count(*) from conn where id=@id";
            SqlParameter[] para = new SqlParameter[]
            {
                new SqlParameter("@id",id)
            };
            if ((int)helper.helper.Execute(sql, para) != 0)
            {
                x = id;
            }
            else
            {
                Id();
            }
            return x;
        }

        private void but_delete_Click(object sender, EventArgs e)
        {
            string klmc = tex_Namespace.Text.Trim();
            string lx = tex_type.Text;
            string bd = Bd();
            string sj = tex_day.Text;
            string kzcs = tex_number.Text;
            string ksj = tex_time.Text;
            string sql = "delete conn where klmc=@klmc and lx=@lx and sj=@sj and bd=@bd and kzcs=@kzcs and ksj=@ksj";
            SqlParameter[] para = new SqlParameter[]
            {
               new SqlParameter("@lx",lx),
               new SqlParameter("@sj",sj),
               new SqlParameter("@bd",bd),
               new SqlParameter("@kzcs",kzcs),
               new SqlParameter("@ksj",ksj),
               new SqlParameter("@klmc",klmc)
            };
            if (helper.helper.Result(sql, para))
            {
                MessageBox.Show("删除成功！");
            }
            else
            {
                MessageBox.Show("删除失败！");
            }
        }
    }
}
